package com.tcd.explore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.tcd.explore.pojo.CarDamageInfo;
import com.tcd.explore.service.CarDamageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/generator")
public class CarDamageInfoController {
    @Autowired
    CarDamageInfoService cardamageInfo;

    @RequestMapping("/cardamageinfoall")
    public List<CarDamageInfo> cardamageinfo(String claimId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("`claim_main_id` ", claimId);
        return cardamageInfo.list(wrapper);
    }

    @RequestMapping("/cardamageinfoadd")
    public Integer cardamageinfoadd(@RequestBody CarDamageInfo cardamage) {
        Long id = IdWorker.getId();
        String num = 440 + "" + id.intValue();
        Integer i = 0;
        if (num.indexOf('-') != -1) {
            num = num.substring(0, num.lastIndexOf("-")) + num.substring(num.lastIndexOf("-") + 1, num.length() - 1);
        }
        i = Integer.parseInt(num.substring(0, 9));
        cardamage.setCardamageId(Integer.parseInt(num.substring(0, 9)));
        return cardamageInfo.saveOrUpdate(cardamage) == true ? i : 0;
    }

    @RequestMapping("/cardamagestatusedit")
    public boolean cardamagestatusedit(@RequestBody CarDamageInfo cardamage) {
        return cardamageInfo.updateById(cardamage);
    }

    @RequestMapping("/cardamagedel")
    public boolean cardamagedel(String cardamageid) {
        return cardamageInfo.removeById(cardamageid);
    }

    @RequestMapping("/cardamageedit")
    public boolean cardamageedit(@RequestBody CarDamageInfo carDamageInfo) {
        return cardamageInfo.updateById(carDamageInfo);
    }
}
